import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Paint;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.SubCategoryAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.GroupedStackedBarRenderer;
import org.jfree.data.KeyToGroupMap;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.StandardGradientPaintTransformer;


public class StackedBarChartDemo4 extends ApplicationFrame {
	final JFreeChart chart;
	
	public StackedBarChartDemo4(final String title) {
		super(title);
		final CategoryDataset dataset = createDataset();
		chart = createChart(dataset);
		final ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new java.awt.Dimension(700, 400));
		setContentPane(chartPanel);
	}


	private CategoryDataset createDataset() {
		DefaultCategoryDataset result = new DefaultCategoryDataset();

		result.addValue(2, "Product 1 (US)", "");
		result.addValue(2, "Product 1 (Europe)", "");
		result.addValue(2, "Product 1 (Asia)", "");
		result.addValue(2, "Product 1 (Middle East)", "");

		result.addValue(2, "Product 2 (US)", "");
		result.addValue(2, "Product 2 (Europe)", "");
		result.addValue(2, "Product 2 (Asia)", "");
		result.addValue(2, "Product 2 (Middle East)", "");

		result.addValue(2, "Product 3 (US)", "");
		result.addValue(2, "Product 3 (Europe)", "");
		result.addValue(2, "Product 3 (Asia)", "");
		result.addValue(2, "Product 3 (Middle East)", "");
		result.addValue(2, "Product 3 (Middle West)", "");

		return result;
	}

	
	private JFreeChart createChart(final CategoryDataset dataset) {

		final JFreeChart chart = ChartFactory.createStackedBarChart(
				"Team Work Load", // chart title
				"", // domain axis label
				"No of Icns", // range axis label
				dataset, // data
				PlotOrientation.VERTICAL, // the plot orientation
				true, // legend
				true, // tooltips
				false // urls
				);

		GroupedStackedBarRenderer renderer = new GroupedStackedBarRenderer();
		KeyToGroupMap map = new KeyToGroupMap("G1");
		map.mapKeyToGroup("Product 1 (US)", "G1");
		map.mapKeyToGroup("Product 1 (Europe)", "G1");
		map.mapKeyToGroup("Product 1 (Asia)", "G1");
		map.mapKeyToGroup("Product 1 (Middle East)", "G1");
		map.mapKeyToGroup("Product 2 (US)", "G2");
		map.mapKeyToGroup("Product 2 (Europe)", "G2");
		map.mapKeyToGroup("Product 2 (Asia)", "G2");
		map.mapKeyToGroup("Product 2 (Middle East)", "G2");
		map.mapKeyToGroup("Product 3 (US)", "G3");
		map.mapKeyToGroup("Product 3 (Europe)", "G3");
		map.mapKeyToGroup("Product 3 (Asia)", "G3");
		map.mapKeyToGroup("Product 3 (Middle East)", "G3");
		map.mapKeyToGroup("Product 3 (Middle West)", "G3");

		renderer.setSeriesToGroupMap(map);

		renderer.setItemMargin(0);
		Paint p1 = new GradientPaint(0.0f, 0.0f, new Color(0x22, 0x22, 0xFF),
				0.0f, 0.0f, new Color(0x88, 0x88, 0xFF));
		renderer.setSeriesPaint(0, p1);
		renderer.setSeriesPaint(4, p1);
		renderer.setSeriesPaint(8, p1);

		Paint p2 = new GradientPaint(0.0f, 0.0f, new Color(0x22, 0xFF, 0x22),
				0.0f, 0.0f, new Color(0x88, 0xFF, 0x88));
		renderer.setSeriesPaint(1, p2);
		renderer.setSeriesPaint(5, p2);
		renderer.setSeriesPaint(9, p2);

		Paint p3 = new GradientPaint(0.0f, 0.0f, new Color(0xFF, 0x22, 0x22),
				0.0f, 0.0f, new Color(0xFF, 0x88, 0x88));
		renderer.setSeriesPaint(2, p3);
		renderer.setSeriesPaint(6, p3);
		renderer.setSeriesPaint(10, p3);

		Paint p4 = new GradientPaint(0.0f, 0.0f, new Color(0xFF, 0xFF, 0x22),
				0.0f, 0.0f, new Color(0xFF, 0xFF, 0x88));
		renderer.setSeriesPaint(3, p4);
		renderer.setSeriesPaint(7, p4);
		renderer.setSeriesPaint(11, p4);
		renderer
				.setGradientPaintTransformer(new StandardGradientPaintTransformer(
						GradientPaintTransformType.HORIZONTAL));

		SubCategoryAxis domainAxis = new SubCategoryAxis("Req Engr Names");
		domainAxis.setCategoryMargin(1);
		domainAxis.addSubCategory("Product 1");
		domainAxis.addSubCategory("Product 2");
		domainAxis.addSubCategory("Product 3");

		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		plot.setDomainAxis(domainAxis);
		plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
		plot.setRenderer(renderer);		
		return chart;

	}

	
	public static void main(final String[] args) {
		final StackedBarChartDemo4 demo = new StackedBarChartDemo4(
				"Stacked Bar Chart Demo 4");
		demo.pack();
		RefineryUtilities.centerFrameOnScreen(demo);
		demo.setVisible(true);
	}

}
